package cn.lyq.card.data.http.cookie;

import androidx.annotation.NonNull;


import com.blankj.utilcode.util.Utils;

import java.util.List;

import okhttp3.Cookie;
import okhttp3.CookieJar;
import okhttp3.HttpUrl;

public class CookiesManager implements CookieJar {
    private static CookiesManager instance;

    private CookiesManager() {
    }

    public static CookiesManager getInstance() {
        if (instance == null) {
            instance = new CookiesManager();
        }
        return instance;
    }

    private final PersistentCookieStore cookieStore = new PersistentCookieStore(Utils.getApp());

    @Override
    public void saveFromResponse(@NonNull HttpUrl url, @NonNull List<Cookie> cookies) {
        if (cookies.size() > 0) {
            for (Cookie item : cookies) {
                cookieStore.add(url, item);
            }
        }
    }

    @Override
    public List<Cookie> loadForRequest(@NonNull HttpUrl url) {
        return cookieStore.get(url);
    }
}
